<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$myPdo = DbUtils::createPdoInst();
$action = $_POST['action'];
try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'add':
      add($myPdo);
      break;
    case 'update':
      update($myPdo);
      break;
    default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo $e->getMessage();
}

function add($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  DbUtils::add($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)) . '_item', $q);
  echo json_encode(array());
}

function update($myPdo) {
  $q = json_decode(MiscUtils::getParam('q', '[]'));
  foreach ($q as $obj) {
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)) . '_item', $obj);
  }
  echo json_encode(array());
}
?>